package com.example.bigevent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.bigevent.pojo.Category;
import com.example.bigevent.mapper.CategoryMapper;
import com.example.bigevent.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bigevent.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    /**
     * 新增文章分类
     * @param category
     */
    @Override
    public void add(Category category) {
        //根据线程获取id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        save(category);
    }
    /**
     * 文章分类列表
     * @return
     */
    @Override
    public List<Category> listCategory() {
        //根据线程获取id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return list(new LambdaQueryWrapper<Category>().eq(Category::getCreateUser,userId));
    }
    /**
     * 更新文章分类
     * @param category
     * @return
     */
    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        updateById(category);
    }
}
